/**
 * 
 */
package it.fuesi.myseries.model.adapters;

import it.fuesi.myseries.R;
import it.fuesi.myseries.model.Episode;
import it.fuesi.myseries.model.Season;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

/**
 * @author fuesi
 * 
 */
public class SeasonExpandableListAdapter extends BaseExpandableListAdapter {

	private Context ctx;
	private List<Season> seasons;

	public SeasonExpandableListAdapter(Context context,List<Season> seasons) {
		//super(context,null,R.layout.season_list_item,R.layout.season_list_item,null, null,null,R.id.episode_name,null,null);
		this.ctx = context;
		this.seasons = seasons;
	}

	
	
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		View v = convertView;

		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) ctx
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.episode_list_item, parent, false);
		}

		TextView itemName = (TextView) v.findViewById(R.id.episode_name);

		final Season season = seasons.get(groupPosition);
		
		final Episode det = seasons.get(groupPosition).getEpisodes()
				.get(childPosition);

		itemName.setText(season.getNumber() + "x" + det.getNumber() + " - " + det.getName());

		return v;

	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		View v = convertView;

		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) ctx
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.season_list_item, parent, false);
		}

		TextView groupName = (TextView) v.findViewById(R.id.season_name);

		Season season = seasons.get(groupPosition);

		groupName.setText("Season "+season.getNumber());

		return v;

	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return seasons.get(groupPosition).getEpisodes().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return (long)seasons.get(groupPosition).getEpisodes().get(childPosition).getNumber();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return seasons.get(groupPosition).getEpisodes().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return seasons.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return seasons.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return seasons.get(groupPosition).getNumber();
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public void addSeason(Season s) {
		seasons.add(s);
		notifyDataSetChanged();
	}

	public void clear() {
		seasons.clear();
		notifyDataSetChanged();
	}
	
	

}
